﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoGear.BLL;
using ECommerce.Library;

public partial class controls_wucProductRecent : System.Web.UI.UserControl
{
    List<Product> _RecentlyViewedProducts;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadSession();
            BindData();
        }
    }

    protected void LoadSession()
    {
        _RecentlyViewedProducts = AutoGearSessionManager.RecentlyViewedProducts;     
    }

    protected void BindData()
    {
        List<Product> tempProducts = new List<Product>();

        if (_RecentlyViewedProducts.Count > 1)
        {
            int count = 1;
            for (int i = this._RecentlyViewedProducts.Count - 2; i >= 0; i--)
            {
                if (count > 3)
                    break;

                tempProducts.Add(this._RecentlyViewedProducts[i]);
                count++;
            }

            rptProductRecent.DataSource = tempProducts;
            rptProductRecent.DataBind();
        }
    }

    protected void rptProductRecent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Product productItem = (Product)e.Item.DataItem;
         
            Image imgItemImage = (Image)e.Item.FindControl("imgItemImage");
            imgItemImage.ImageUrl = ImageResizer.CheckImageMissing(productItem.ProductImageSmallThumbnail, AutoGear.BLL.SiteSettings.NoImageSmallThumbnailUrl);

            HyperLink hlkItem = (HyperLink)e.Item.FindControl("hlkItem");
            hlkItem.NavigateUrl = productItem.ProductUrl;
            hlkItem.Text = productItem.ProductName;

            Label lblItemPrice = (Label)e.Item.FindControl("lblItemPrice");
            lblItemPrice.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(productItem.ProductPrice);

            //    HiddenField hdfProductId = (HiddenField)e.Item.FindControl("hdfProductId");
            //    hdfProductId.Value = productItem.ProductId.ToString();

            //    Label lblItemNumber = (Label)e.Item.FindControl("lblItemNumber");
            //    lblItemNumber.Text = "Item Number: " + productItem.ItemNumber;

            //    TextBox tbxQuantity = (TextBox)e.Item.FindControl("tbxQuantity");
            //    tbxQuantity.Text = cartItem.Quantity.ToString();
            //    tbxQuantity.MaxLength = SiteUtility.MaximumQuantityLength;

            //    Label lblItemTotal = (Label)e.Item.FindControl("lblItemTotal");
            //    lblItemTotal.Text = ECommerce.Library.ECommerce.Utility.FormatPriceText(cartItem.ItemTotal);

            //    ImageButton imbItemUpdate = (ImageButton)e.Item.FindControl("imbItemUpdate");
            //    imbItemUpdate.CommandArgument = e.Item.ItemIndex.ToString();
        }
    }

    protected void rptProductRecent_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    }
}
